﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OTS.BLL;

public partial class SysManage_Company_BUserAndCoM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
            Session["selectingdtlist"] = null;
            Session["selecteddtlist"] = null;
            //  权限
            SettingRight();
            // 初始化
            BindComany();
            InitSelectedDataBind(); 
            InitSelectingDataBind();
        }
    }
    // 页面权限
    private void SettingRight()
    {
        bool[] rightlist = SysRightBLL.GetRightByType(new string[] { 
            Session["UserCode"].ToString(), 
            Session["CoCode"].ToString(), 
            "BUserCo"});
        if (!rightlist[0])
        { this.Page.Response.Write(String.Format("<script language='javascript'>window.parent.location.href='{0}';</script>", CommonModel.loginOut2)); }

        gvselected.Columns[gvselected.Columns.Count -1 ].Visible = rightlist[3];
        gvselecting.Columns[gvselecting.Columns.Count - 1].Visible = rightlist[2];
    }
    //绑定公司
    public void BindComany()
    {
        Login login = new Login();
        ddlcompany.DataSource = login.GetCompany();
        ddlcompany.DataValueField = "CoCode";
        ddlcompany.DataTextField = "CoName";
        ddlcompany.DataBind();
    }
    // 已有数据
    private void InitSelectedDataBind()
    {
        DataTable dtlist = new DataTable();
        if (Session["selecteddtlist"] != null)
        {
            dtlist = Session["selecteddtlist"] as DataTable;
        }
        else
        {
            if (ddlcompany != null && ddlcompany.Items.Count > 0)
                dtlist = UserAndCoBLL.GetCompanySelectedUser(ddlcompany.SelectedValue);
        }
        gvselected.DataSource = dtlist;
        gvselected.DataBind();
        Session["selecteddtlist"] = dtlist;
    }
    private void InitSelectingDataBind()
    {
        DataTable dtlist = new DataTable();
        if (Session["selectingdtlist"] != null)
        {
            dtlist = Session["selectingdtlist"] as DataTable;
        }
        else
        {
            string temp = "";
            if (ddlcompany != null && ddlcompany.Items.Count > 0)
                temp = ddlcompany.SelectedValue;
            dtlist = UserAndCoBLL.GetCompanySelectingUser(temp);
        }
        gvselecting.DataSource = dtlist;
        gvselecting.DataBind();
        Session["selectingdtlist"] = dtlist;
    }
    
    protected void ddlcompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["selectingdtlist"] = null;
        Session["selecteddtlist"] = null;
        InitSelectedDataBind();
        InitSelectingDataBind();
    }

    protected void gvselecting_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitSelectingDataBind();
    }
    protected void gvselecting_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        UserCoModel model = new UserCoModel();
        model.CoCode = ddlcompany.SelectedValue;
        model.UserCode = e.KeyValue.ToString();
        model.UserCoDesc = "";
        model.UpdateAuthor = Session["UserCode"].ToString();
        model.UpdateDate = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

        if (UserAndCoBLL.AddUserCo(model) > 0)
        {
            CommonMethod.regAlertMessage(Upd, "操作成功!", this);
            Session["selectingdtlist"] = null;
            Session["selecteddtlist"] = null;
            InitSelectedDataBind();
            InitSelectingDataBind();
        }
        else
        {
            CommonMethod.regAlertMessage(Upd, "出现错误!", this);
        }
    }
    protected void gvselected_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitSelectedDataBind();
    }
    protected void gvselected_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);

        UserCoModel model = new UserCoModel();
        model.CoCode = ddlcompany.SelectedValue;
        model.UserCode = e.KeyValue.ToString();
        model.UserCoDesc = "";
        model.UpdateAuthor = Session["UserCode"].ToString();
        model.UpdateDate = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

        if (UserAndCoBLL.DeleteUserCo(model) > 0)
        {
            CommonMethod.regAlertMessage(Upd, "操作成功!", this);
            Session["selectingdtlist"] = null;
            Session["selecteddtlist"] = null;
            InitSelectedDataBind();
            InitSelectingDataBind();
        }
        else
        {
            CommonMethod.regAlertMessage(Upd, "出现错误!", this);
        }
    }

}